package dataservice;

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

import Enum.ColumnName;
import nju.edu.super_scholar.vo.FriendsInfoVO;
import nju.edu.super_scholar.vo.FriendsVo;
import nju.edu.super_scholar.vo.PersonalInfoVO;
import dBInfo.DeleteLimitation;
import dBInfo.GetTableLimitation;
import dBInfo.InsertInformation;
import dBInfo.InsertLimitation;
import dBInfo.Limitation;
import dBInfo.UpdateLimitation;
import databaseService.RelationDataService;
import dataserviceImp.FriendImp;

public class FriendInfoOp implements FriendImp {
	private RelationDataService relationDataService;

	public FriendInfoOp() {
		// TODO Auto-generated constructor stub
		relationDataService = new RelationDataService();
	}

	@Override
	public void addFriend(FriendsVo friendsVo) throws RemoteException {
		// TODO Auto-generated method stub
		ArrayList<Object> infoList = new ArrayList<Object>();
		String hostInfo = friendsVo.getHostId();
		String guestId = friendsVo.getGuestId();
		infoList.add(hostInfo);
		infoList.add(guestId);
		infoList.add(1);
		InsertInformation information = new InsertInformation(infoList);
		InsertLimitation insertLimitation = new InsertLimitation(information);
		relationDataService.insert(insertLimitation);
	}

	@Override
	public void deleteFriend(FriendsVo friendsVo) throws RemoteException {
		// TODO Auto-generated method stub
		String hostInfo = friendsVo.getHostId();
		String guestInfo = friendsVo.getGuestId();
		Limitation limitation = new Limitation();
		limitation.setLimitation(ColumnName.hostId, hostInfo);
		limitation.setLimitation(ColumnName.guestId, guestInfo);
		DeleteLimitation deleteLimitation = new DeleteLimitation(limitation);
		relationDataService.deleteInfo(deleteLimitation);
	}

	@Override
	public ArrayList<FriendsInfoVO> downLoadMyFriendList(String hostId)
			throws RemoteException {
		// TODO Auto-generated method stub
		ArrayList<FriendsInfoVO> friendsInfo = new ArrayList<FriendsInfoVO>();
		Limitation limitation = new Limitation();
		limitation.setLimitation(ColumnName.hostId, hostId);
		ArrayList<ColumnName> arrayList = new ArrayList<ColumnName>();
		arrayList.add(ColumnName.guestId);
		GetTableLimitation getTableLimitation = new GetTableLimitation(
				limitation, arrayList);
		DefaultTableModel defaultTableModel = relationDataService
				.getTable(getTableLimitation);

		for (int i = 0; i < defaultTableModel.getRowCount(); i++) {
			PersonalInfoOp personalInfoOp = new PersonalInfoOp();
			PersonalInfoVO personalInfoVO = personalInfoOp
					.checkMyInfo(defaultTableModel.getValueAt(i, 0).toString());
			FriendsInfoVO friendsInfoVO = new FriendsInfoVO(
					personalInfoVO.getId(), personalInfoVO.getNickname(),
					personalInfoVO.getAchivement(), personalInfoVO.getGender(),
					personalInfoVO.getLevel());
           friendsInfo.add(friendsInfoVO);  
		}

		return friendsInfo;
	}

	@Override
	public void setFresh(FriendsVo friendsVo) throws RemoteException {
		// TODO Auto-generated method stub
          Limitation limitation=new Limitation();
          limitation.setLimitation(ColumnName.guestId, friendsVo.getGuestId());
          int fresh=0;
          if (friendsVo.isGuestFreshed()) {
			fresh=1;
		}
          else {
			fresh=0;
		}
          limitation.setUpdate(ColumnName.refresh,fresh );
          UpdateLimitation updateLimitation=new UpdateLimitation(limitation);
          relationDataService.update(updateLimitation);
          
	}

	@Override
	public ArrayList<FriendsInfoVO> getFreshList(String hostId)
			throws RemoteException {
		// TODO Auto-generated method stub
		ArrayList<FriendsInfoVO> friendsInfo = new ArrayList<FriendsInfoVO>();
		Limitation limitation = new Limitation();
		
		limitation.setLimitation(ColumnName.hostId, hostId);
		ArrayList<ColumnName> arrayList = new ArrayList<ColumnName>();
		arrayList.add(ColumnName.guestId);
		arrayList.add(ColumnName.refresh);
		GetTableLimitation getTableLimitation = new GetTableLimitation(
				limitation, arrayList);
		DefaultTableModel defaultTableModel = relationDataService
				.getTable(getTableLimitation);
		
		for (int i = 0; i < defaultTableModel.getRowCount(); i++) {
			if (Integer.parseInt(defaultTableModel.getValueAt(i,1).toString())==1) {
				PersonalInfoOp personalInfoOp = new PersonalInfoOp();
				PersonalInfoVO personalInfoVO = personalInfoOp
						.checkMyInfo(defaultTableModel.getValueAt(i, 0).toString());
				FriendsInfoVO friendsInfoVO = new FriendsInfoVO(
						personalInfoVO.getId(), personalInfoVO.getNickname(),
						personalInfoVO.getAchivement(), personalInfoVO.getGender(),
						personalInfoVO.getLevel());
	           friendsInfo.add(friendsInfoVO);
	           FriendsVo friendsVo=new FriendsVo(hostId, personalInfoVO.getId(), false,false);
	           setFresh(friendsVo);
			}
			
		}

		return friendsInfo;
	}

}
